package com.ruoyi.project.util;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.project.constant.AliSmsConstant;
import com.ruoyi.project.constant.InterfaceStatus;
import com.ruoyi.project.constant.statuEnum.SmsTypeEnum;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @program: lvhang
 * @description: 旅航项目工具类
 * @author: Mr.Wang
 * @create: 2020-06-28 15:45
 **/
public class LvHangHttpUtil {

    /**
     * 发送短信
     *
     * @param phone    手机号
     * @param typeEnum 短信类型
     * @return
     * @throws Exception
     */
    public static void sendMsg(String phone, SmsTypeEnum typeEnum, JSONObject data) throws Exception {

        String template_code = "";
        switch (typeEnum.getCode()) {
            case 0: //登录
                template_code = AliSmsConstant.LOGIN_TEMPLATE;
                break;
            case 1: //修改密码
                template_code = AliSmsConstant.UPDATE_PWD_TEMPLATE;
                break;
            case 2: //下单成功
                template_code = AliSmsConstant.SUBMIT_TEMPLATE;
                break;
        }

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                AliSmsConstant.ACCESSKEY_ID, AliSmsConstant.ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", AliSmsConstant.SIGN_NAME);
        request.putQueryParameter("TemplateCode", template_code);
        request.putQueryParameter("TemplateParam", String.valueOf(data));

        CommonResponse response = client.getCommonResponse(request);
        JSONObject jsonObject = (JSONObject) JSONObject.parse(response.getData());
        //短信发送成功
        if (jsonObject.get("Code").equals("isv.MOBILE_NUMBER_ILLEGAL")) {
            throw new CustomException("请输入正确的手机号", InterfaceStatus.ID_CARD_FAIL);
        } else if (!jsonObject.get("Code").equals("OK")) {
            throw new CustomException(jsonObject.get("Message").toString());
        }
    }

}
